package com.bayi.storage.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Wm on 16/8/1/0001.
 */
public class SharedPreferenceUtil {
    public final static String SP_LOCAL = "SP_LOCAL";
    private final static String SP_NAME = "BY_SPF";
    private static SharedPreferenceUtil mSharedPreferenceUtil;
    private static SharedPreferences preferences;

    private SharedPreferenceUtil() {
    }

    public static SharedPreferenceUtil getSharedPreferenceUtil(Context context) {
        if (mSharedPreferenceUtil == null) {
            mSharedPreferenceUtil = new SharedPreferenceUtil();
        }
        if (preferences == null) {
            preferences = context.getSharedPreferences(SP_NAME,
                    Context.MODE_PRIVATE);
        }
        return mSharedPreferenceUtil;
    }

    public String getInfoFromShared(String key) {
        return preferences.getString(key, null);
    }

    /**
     * 获取数据
     *
     * @param key
     * @param defValue
     * @return
     */
    public String getInfoFromShared(String key, String defValue) {
        return preferences.getString(key, defValue);
    }

    /**
     * 获取数据
     *
     * @param key
     * @param defValue
     * @return
     */
    public long getInfoFromShared(String key, long defValue) {
        return preferences.getLong(key, defValue);
    }

    /**
     * 设置数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setInfoToShared(String key, long value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putLong(key, value);
        editor.apply();
        return true;
    }

    /**
     * 设置数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setInfoToShared(String key, String value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.apply();
        return true;
    }

    /**
     * 设置数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean setInfoToShared(String key, boolean value) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
        return true;
    }

    /**
     * 获取数据
     *
     * @param key
     * @param defValue
     * @return
     */
    public boolean getInfoFromShared(String key, boolean defValue) {
        return preferences.getBoolean(key, defValue);
    }


    public <T> void saveObjectBean(final T t, final String key) {
        SharedPreferences.Editor editor = preferences.edit();
        String json = JsonUtil.toJSON(t);
        editor.putString(key, json);
        editor.apply();
    }

    public <T> Object getObjectBean(Class<T> t, String key) {
        String json = preferences.getString(key, "");
        if ("".equals(json)) {
            return null;
        }
        T obt = JsonUtil.toBean(json, t);
        return obt;
    }

    /**
     * 删除指定数据
     *
     * @param key
     */
    public void deleteInfoToShared(String key) {
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(key);
        editor.apply();
    }

    /**
     * 清空数据
     */
    public void clearInfoToShared() {
    }
}
